package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.CustomerMaintainFeeAttachBiz;
import com.egao.orderB.entity.CustomerMaintainFeeAttach;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-07-15 12:46:52
 * @version ：1.0
 */
@Repository
public class CustomerMaintainFeeAttachBizImpl extends BaseDao implements CustomerMaintainFeeAttachBiz {

	@Override
	public JsonResult list(PageResult2<T> page, CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<CustomerMaintainFeeAttach> rm = new BeanPropertyRowMapper<CustomerMaintainFeeAttach>(CustomerMaintainFeeAttach.class);
		StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.name,c.pinyin,c.standard,c.box_type");
		sql.append(",c.price,c.type,c.at_modules,c.longitude");
		sql.append(",c.latitude,c.radius,c.yard_dock,c.remark");
		sql.append(",c.create_time,c.create_user,c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_customer_maintain_fee_attach c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(customerMaintainFeeAttach)){
			if(Utils.isNotEmpty(customerMaintainFeeAttach.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customerMaintainFeeAttach.getName());
			}
			if(Utils.isNotEmpty(customerMaintainFeeAttach.getCustomerId())){
				sql.append(" and instr(c.customer_id,:customer_id)>0 ");
				map.put("customer_id", customerMaintainFeeAttach.getCustomerId()+"");
			}
			if(Utils.isNotEmpty(customerMaintainFeeAttach.getSort())&&Utils.isNotEmpty(customerMaintainFeeAttach.getOrder())){
                sql.append(" order by c."+customerMaintainFeeAttach.getSort()+" "+customerMaintainFeeAttach.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<CustomerMaintainFeeAttach> list(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<CustomerMaintainFeeAttach> rm = new BeanPropertyRowMapper<CustomerMaintainFeeAttach>(CustomerMaintainFeeAttach.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.name,c.pinyin,c.standard,c.box_type");
		sql.append(",c.price,c.type,c.at_modules,c.longitude");
		sql.append(",c.latitude,c.radius,c.yard_dock,c.remark");
		sql.append(",c.create_time,c.create_user,c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_customer_maintain_fee_attach c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(customerMaintainFeeAttach)){
			if(Utils.isNotEmpty(customerMaintainFeeAttach.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customerMaintainFeeAttach.getName());
			}
			if(Utils.isNotEmpty(customerMaintainFeeAttach.getCustomerId())){
				sql.append(" and instr(c.customer_id,:customer_id)>0 ");
				map.put("customer_id", customerMaintainFeeAttach.getCustomerId()+"");
			}
            if(Utils.isNotEmpty(customerMaintainFeeAttach.getSort())&&Utils.isNotEmpty(customerMaintainFeeAttach.getOrder())){
				sql.append(" order by c."+customerMaintainFeeAttach.getSort()+" "+customerMaintainFeeAttach.getOrder());
			} else {
				sql.append(" order by c.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public CustomerMaintainFeeAttach getById(CustomerMaintainFeeAttach customerMaintainFeeAttach) {
		RowMapper<CustomerMaintainFeeAttach> rm = new BeanPropertyRowMapper<CustomerMaintainFeeAttach>(CustomerMaintainFeeAttach.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.name,c.pinyin,c.standard,c.box_type");
		sql.append(",c.price,c.type,c.at_modules,c.longitude");
		sql.append(",c.latitude,c.radius,c.yard_dock,c.remark");
		sql.append(",c.create_time,c.create_user,c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_customer_maintain_fee_attach c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 and c.id=:id ");
		map.put("id", customerMaintainFeeAttach.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
		if(Utils.isNotEmpty(customerMaintainFeeAttach.getStandard())) {
			customerMaintainFeeAttach.setPinyin(PinyinUtils.ToFirstChar(customerMaintainFeeAttach.getName()+customerMaintainFeeAttach.getStandard()));
		} else  {
			customerMaintainFeeAttach.setPinyin(PinyinUtils.ToFirstChar(customerMaintainFeeAttach.getName()));
		}
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_customer_maintain_fee_attach (");
        sql.append("organization_id,customer_id,name,pinyin");
		sql.append(",standard,box_type,price,type");
		sql.append(",at_modules,longitude,latitude,radius");
		sql.append(",yard_dock,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:name,:pinyin");
		sql.append(",:standard,:box_type,:price,:type");
		sql.append(",:at_modules,:longitude,:latitude,:radius");
		sql.append(",:yard_dock,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", customerMaintainFeeAttach.getCustomerId()==null?null:customerMaintainFeeAttach.getCustomerId()+"");
        map.put("name", customerMaintainFeeAttach.getName());
        map.put("pinyin", customerMaintainFeeAttach.getPinyin());
        map.put("standard", customerMaintainFeeAttach.getStandard());
        map.put("box_type", customerMaintainFeeAttach.getBoxType());
        map.put("price", customerMaintainFeeAttach.getPrice());
        map.put("type", customerMaintainFeeAttach.getType());
        map.put("at_modules", customerMaintainFeeAttach.getAtModules());
        map.put("longitude", customerMaintainFeeAttach.getLongitude());
        map.put("latitude", customerMaintainFeeAttach.getLatitude());
        map.put("radius", customerMaintainFeeAttach.getRadius()==null?null:customerMaintainFeeAttach.getRadius()+"");
        map.put("yard_dock", customerMaintainFeeAttach.getYardDock());
        map.put("remark", customerMaintainFeeAttach.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
		customerMaintainFeeAttach.setPinyin(PinyinUtils.ToFirstChar(customerMaintainFeeAttach.getName()+customerMaintainFeeAttach.getStandard()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_customer_maintain_fee_attach set ");
        sql.append("name=:name,pinyin=:pinyin ");
		sql.append(",standard=:standard,box_type=:box_type,price=:price ");
		sql.append(",type=:type,at_modules=:at_modules,longitude=:longitude ");
		sql.append(",latitude=:latitude,radius=:radius,yard_dock=:yard_dock ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", customerMaintainFeeAttach.getName());
        map.put("pinyin", customerMaintainFeeAttach.getPinyin());
        map.put("standard", customerMaintainFeeAttach.getStandard());
        map.put("box_type", customerMaintainFeeAttach.getBoxType());
        map.put("price", customerMaintainFeeAttach.getPrice());
        map.put("type", customerMaintainFeeAttach.getType());
        map.put("at_modules", customerMaintainFeeAttach.getAtModules());
        map.put("longitude", customerMaintainFeeAttach.getLongitude());
        map.put("latitude", customerMaintainFeeAttach.getLatitude());
        map.put("radius", customerMaintainFeeAttach.getRadius()==null?null:customerMaintainFeeAttach.getRadius()+"");
        map.put("yard_dock", customerMaintainFeeAttach.getYardDock());
        map.put("remark", customerMaintainFeeAttach.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customerMaintainFeeAttach.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(customerMaintainFeeAttach.getId().length()>0){
			for(String ID : customerMaintainFeeAttach.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_customer_maintain_fee_attach set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
